package com.hengtiansoft.productcrawl;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.LinkedList;
import java.util.SortedSet;
import java.util.TreeSet;

public class Crawl {
	public static Collection<SellerBean> sellers = new LinkedList<SellerBean>();
	
	static {
		sellers.add(new SellerBean("Homeclick.com", "www.homeclick.com"));
		sellers.add(new SellerBean("AJ Madison", "www.ajmadison.com"));
		sellers.add(new SellerBean("eFaucets", "www.efaucets.com"));
		sellers.add(new SellerBean("FaucetDirect.com", "http://www.faucetdirect.com/"));
	}
	
	public static void main(String[] args) throws Exception {
		if (args.length != 3) {
			System.out.println("Usage: " + args[0] + "infile " + "outfile");
		}
		
		String inFile = args[1];
		String outFile = args[2];
		
		SortedSet<ProductBean> products = new TreeSet<ProductBean>();
		BufferedReader inputStream = null;
		try {
			inputStream = new BufferedReader(new FileReader(inFile));
			
			String l;
			while((l = inputStream.readLine()) != null) {
				String[] ll = l.split(",");
				ProductBean product = new ProductBean(ll[0].trim(), ll[1].trim());
				products.add(product);
			}
		} finally {
			if(inputStream != null) {
				inputStream.close();
			}
		}
		
		SortedSet<PriceBean> prices = new TreeSet<PriceBean>();
		for (SellerBean s : sellers) {
			ProductCrawl crawl = new ProductCrawl(s.getName());
			ProductParser parser = new ProductParser(s);
			for (ProductBean p : products) {
				String page;
				PriceBean priceBean;
				crawl.setProduct(p.getSku());
				page = crawl.query();
				parser.setPage(page);
				priceBean = parser.parse();
				if(priceBean != null) {
					priceBean.setProduct(p);
					prices.add(priceBean);
				}
			}
		}
		
		PrintWriter out = null;
		try {
			out = new PrintWriter(new BufferedWriter(new FileWriter(outFile)));
			for (PriceBean p : prices) {
				out.println(p.toCSV());
			}
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}
}
	